package com.iring.service.common.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iring.domain.config.prop.LocalConfigProperties;
import com.iring.dao.common.CallRateDao;
import com.iring.dao.common.CallRateDetailDao;
import com.iring.domain.common.CallRate;
import com.iring.domain.common.CallRateDetail;
import com.iring.exception.ParamInvalidException;
import com.iring.service.common.CallRateService;
import com.iring.vo.common.CallRateVO;
import com.iring.vo.common.RateVO;

/**
 * @author wanggan
 */
@Service
public class CallRateServiceImpl implements CallRateService {

    @Autowired
    private CallRateDetailDao callRateDetailDao;

    @Autowired
    private CallRateDao callRateDao;

    @Autowired
    LocalConfigProperties localConfigProperties;

    /**
     * @see CallRateService#listAllCountryCallrate()
     */
    @Override
    public List<CallRateVO> listAllCountryCallrate() {

        List<CallRate> callRateList = callRateDao.findByIsshowIsTrue();
        List<CallRateVO> callRateVOList = new ArrayList<>();

        String rateGroup = localConfigProperties.getRateGroup();

        callRateList.stream()
                .forEach(index->{
                    CallRateVO callRateVO = new CallRateVO();
                    callRateVO.setId(index.getId());
                    callRateVO.setCode(index.getCode());

                    List<String> country = new ArrayList<>();
                    country.add(index.getCnname());
                    country.add(index.getEnname());
                    callRateVO.setCountry(country);

                    callRateVO.setCycle(index.getCycle());
                    if("overseas".equalsIgnoreCase(rateGroup)){
                        callRateVO.setRate(index.getEnRate());
                    }else if("domestic".equalsIgnoreCase(rateGroup)){
                        callRateVO.setRate(index.getCnRate());
                    }
                    callRateVOList.add(callRateVO);
                });

        return callRateVOList;
    }

    /**
     * @see CallRateService#listSingleCountryCallrateDeatil(RateVO)
     */
    @Override
    public List<CallRateVO> listSingleCountryCallrateDeatil(RateVO rateVO) {
        if (null == rateVO.getId()) {
            throw new ParamInvalidException("id is null");
        }

        String rateGroup = localConfigProperties.getRateGroup();

        List<CallRateDetail> callRateDetailList =  callRateDetailDao.findByPid(rateVO.getId());
        List<CallRateVO> callRateVOList = new ArrayList<>();

        callRateDetailList.stream()
                .forEach(index->{
                    CallRateVO callRateVO = new CallRateVO();
                    callRateVO.setId(index.getId());
                    callRateVO.setCode(index.getCode());

                    List<String> country = new ArrayList<>();
                    country.add(index.getCnname());
                    country.add(index.getEnname());
                    callRateVO.setCountry(country);

                    callRateVO.setCycle(index.getCycle());
                    if("overseas".equalsIgnoreCase(rateGroup)){
                        callRateVO.setRate(index.getEnRate());
                    }else if("chinese".equalsIgnoreCase(rateGroup)){
                        callRateVO.setRate(index.getCnRate());
                    }

                    callRateVOList.add(callRateVO);
                });

        return callRateVOList;
        // 查询指定国家的费率信息
    }
}
